<template>
  <div class="login-container">
    <div class="login-bg"></div>
    <el-card class="login-card" shadow="always">
      <div class="logo-container">
        <svg class="repair-icon" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
          <path d="M50 15L65 30L35 60L20 45L50 15Z" fill="#FF6B35" />
          <path d="M50 25L58 33L38 53L30 45L50 25Z" fill="#FFF" />
          <circle cx="50" cy="70" r="15" fill="#297062" />
          <path d="M45 65L55 75M55 65L45 75" stroke="#FFF" stroke-width="3" />
        </svg>
        <h1 class="system-name">PERFECT REPAIR</h1>
      </div>
      <h2 class="login-title">用户注册</h2>
        <div class="repair-info">
          <p class="info-text">创建账号，开始使用我们的维修管理服务</p>
        </div>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="0" class="register-form">
        <el-form-item prop="username">
          <div class="input-wrapper">
            <el-icon class="input-icon"><User /></el-icon>
            <el-input v-model="form.username" placeholder="请输入用户名" /></div>
        </el-form-item>
        <el-form-item prop="password">
          <div class="input-wrapper">
            <el-icon class="input-icon"><Lock /></el-icon>
            <el-input type="password" v-model="form.password" placeholder="请输入密码" show-password /></div>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <div class="input-wrapper">
            <el-icon class="input-icon"><Lock /></el-icon>
            <el-input type="password" v-model="form.confirmPassword" placeholder="请再次输入密码" show-password /></div>
        </el-form-item>
        <el-form-item prop="invitationCode">
          <div class="input-wrapper">
            <el-icon class="input-icon"><Key /></el-icon>
            <el-input v-model="form.invitationCode" placeholder="请输入邀请码（选填）" /></div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit" :loading="loading" class="login-btn">
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="register-link">
          <span>已有账号？</span>
          <el-button type="text" @click="switchTab" class="link-btn">前往登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import service from '@/axios'
import { ElCard, ElForm, ElFormItem, ElInput, ElButton, ElIcon } from 'element-plus'
import { User, Lock, Key } from '@element-plus/icons-vue'
import { toast } from '~/composables/util'

const router = useRouter()
const formRef = ref(null)
const loading = ref(false)

const form = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  invitationCode: ''
})

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value) => {
        return value === form.password || '两次输入密码不一致'
      },
      trigger: 'blur'
    }
  ]
})

const handleSubmit = async () => {
  await formRef.value.validate()
  loading.value = true
  try {
    // 调用注册API
    const response = await service.post('/api/auth/register/user', {
      username: form.username,
      password: form.password,
      invitationCode: form.invitationCode
    })
    console.log(response)
  
    if (response?.data?.code === 200) {
      toast(response?.data?.message || '注册成功')
      router.push('/login')
    } else {
      toast('注册失败: ' + (response?.data?.message || '未知错误'), 'error')
    }
  } catch (err) {
    console.error('注册错误详情:', err)
    toast('注册失败: ' + (err.response?.data?.message || err.message || '网络错误'), 'error')
  } finally {
    loading.value = false
  }
}

const switchTab = () => {
  router.push('/login')
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  z-index: 1;
}

.login-card {
  position: relative;
  z-index: 2;
  width: 420px;
  border-radius: 16px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  background-color: rgba(255, 255, 255, 0.95);
  padding: 30px;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.repair-icon {
  width: 50px;
  height: 50px;
  margin-right: 15px;
}

.system-name {
  font-size: 28px;
  font-weight: 700;
  color: #297062;
  margin: 0;
}

.login-title {
  text-align: center;
  margin-bottom: 15px;
  color: #333;
  font-size: 20px;
  font-weight: 600;
}

.repair-info {
  text-align: center;
  margin-bottom: 30px;
}

.info-text {
  color: #606266;
  font-size: 14px;
  line-height: 1.6;
  max-width: 350px;
  margin: 0 auto;
}

.input-wrapper {
  display: flex;
  align-items: center;
  border: 1px solid #e4e7ed;
  border-radius: 22px;
  padding: 0 20px;
  width: 480px;
  height: 48px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
}

.input-wrapper:focus-within {
  border-color: #297062;
  box-shadow: 0 0 0 3px rgba(41, 112, 98, 0.1);
  transform: translateY(-2px);
}

.input-icon {
  color: #297062;
  margin-right: 12px;
  font-size: 18px;
  transition: color 0.3s;
}

.input-wrapper:focus-within .input-icon {
  color: #297062;
}

.login-btn {
  width: 100%;
  height: 48px;
  border-radius: 24px;
  font-size: 16px;
  background-color: #297062;
  border: none;
  transition: all 0.3s;
}

.login-btn:hover {
  background-color: #1f5a4f;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(41, 112, 98, 0.3);
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #606266;
  font-size: 14px;
}

.link-btn {
  color: #297062 !important;
  padding: 0;
  margin-left: 5px;
}

.link-btn:hover {
  color: #1f5a4f !important;
  text-decoration: underline;
}
</style>
